package com.qiuzhiyi.activity;

import android.widget.TextView;

import com.li.utils.TransformUtil;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.R;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.MsgListItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2018/1/11 0011
 * Description: 消息详情
 */

public class MsgDetailActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.txt_msg_title)
    TextView mTxtTitle;
    @BindView(R.id.txt_time)
    TextView mTxtTime;
    @BindView(R.id.txt_author)
    TextView mTxtAuthor;
    @BindView(R.id.txt_content)
    TextView mTxtContent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg_detail;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        String id=getIntent().getStringExtra("newsId");
        showMsgDetail(id);
    }

    private void showMsgDetail(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("nId", id);
        ObservableHelper.createObservable(RetrofitHelper.createService().getMsgDetail(map))
                .subscribe(new SelectorObserver<BaseItem<MsgListItem>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseItem<MsgListItem> item) {
                        MsgListItem listItem = item.getObjValue();
                        String author = listItem.getNewsAuthor();
                        String time = TransformUtil.timeFormat(listItem.getUpdateTime(), "yyyy-MM-dd");
                        String content = listItem.getNewsContent();

                        mTxtTitle.setText(listItem.getNewsTitle());
                        mTxtTime.setText(time);
                        mTxtAuthor.setText(author);
                        mTxtContent.setText(content);
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoadingDialog();
                        addDisposable(d);
                    }
                });
    }
}
